﻿$(function () {
    $("#GetSong").click(function () {      
        var request = $.ajax({
            type: 'GET',
            dataType: 'json',
            url: '/api/music?$orderby=' + "songName" + ',' + "songName"
        });
        request.done(function (data) {
            if (data.length != 0) {
                appendSongs(data);
            }
        });
        request.fail(function (jqXHR, textStatus) {
            alert("Request failed(getSongs): " + textStatus);
        });
    });

    // a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
    $("#dialog:ui-dialog").dialog("destroy");

    var songSource = $("#data"),
			allFields = $([]).add(songSource),
			tips = $(".validateTips");

    function updateTips(t) {
        tips
				.text(t)
				.addClass("ui-state-highlight");
        setTimeout(function () {
            tips.removeClass("ui-state-highlight", 1500);
        }, 500);
    }

    function checkLength(o, n, min, max) {
        if (o.val().length > max || o.val().length < min) {
            o.addClass("ui-state-error");
            updateTips("Length of " + n + " must be between " +
					min + " and " + max + ".");
            return false;
        } else {
            return true;
        }
    }
    function custom(o, n, min, max) {
        if (o.val().length > max || o.val().length < min) {
            o.addClass("ui-state-error");
            updateTips("Music file is required.");
            return false;
        } else {
            return true;
        }
    }

    function checkRegexp(o, regexp, n) {
        if (!(regexp.test(o.val()))) {
            o.addClass("ui-state-error");
            updateTips(n);
            return false;
        } else {
            return true;
        }
    }

    $("#dialog-form").dialog({
        autoOpen: false,
        height: 300,
        width: 350,
        modal: true,
        buttons: {
            "Upload Song": function () {
                var bValid = true;
                allFields.removeClass("ui-state-error");

                //bValid = bValid && checkLength(songName, "song name", 3, 30);
                //bValid = bValid && checkLength(songArtist, "song artist", 3, 30);
                bValid = bValid && custom(songSource, "song source", 1, 100);
                //bValid = bValid && checkRegexp(songName, /^[a-z]([0-9a-z_])+$/i, "Name may consist of a-z, 0-9, underscores, begin with a letter.");
                // From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/

                if (bValid) {
//                    var sname = $("#name").val();
//                    var sartist = $("#artist").val();
//                    var ssource = $("#source").val();

//                    var obj = { Id: "", name: sname, artist: sartist, source: ssource, ext: "Ext" };
//                    var request = $.ajax({
//                        url: "/api/music/",
//                        type: "PUT",
//                        data: { song: obj },
//                        dataType: "html"
//                    });
//                    request.done(function () {
//                        alert("Song Added!")
//                        //request.done(function (msg) {
//                        //    $("#log").html(msg);
//                        loadPlayer();
//                    });

//                    request.fail(function (jqXHR, textStatus) {
//                        alert("Request failed: " + textStatus);
                    //                    });
                    $('#theForm').submit();
                    //loadPlayer();
                    //$('#progress').show();
                    //$(this).dialog("close");
                }
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function () {
            allFields.val("").removeClass("ui-state-error");
            loadPlayer();
        }
    });

    $("#uploadSong").click(function () {
        //			    if (shapes.length < 1) {
        //			        alert("Nothing to save!");
        //			        return;
        //			    }
        $("#dialog-form").dialog("open");
    });

    function loadPlayer() {
        $('#musicPlayer').empty();
        //alert("Getting song");
        $.getJSON("/api/music", function (data) {
            //alert(data.songName + " " + data.songArtist + " " + data.songSource);

            musicList = [];
            for (var i = 0; i < data.length; i++) {
                var obj = { src: data[i].songSource, name: data[i].songName + " - " + data[i].songArtist, length : data[i].songLength  };
                musicList.push(obj);
            }
            var myOptions = { autoPlay: false,
                erroMessage: "This is the user custon error message",
                //albumCover: "../../Content/Music/IV/AlbumArtSmall.jpg",
                albumName: "Others",
                artistName: "Various"
            };

            $('#musicPlayer').musicPlayer(musicList, myOptions).hide().fadeIn(1500);


        });
    }
});